package com.fh.util;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件上传类
 * @author fc
 *
 */
public class Upload {

	private   HttpServletRequest request;
	
	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public Upload(HttpServletRequest request) {
		super();
		this.request = request;
	}

	/**
	 * 保存单个文件
	 * @param file
	 * @param request
	 * @return
	 */
	public String upload(MultipartFile file) {
        String path = request.getSession().getServletContext().getRealPath("upload");  
        String fileName = getUUID()+getExtName(file);
        File targetFile = new File(path, fileName);  
        if(!targetFile.exists()){  
            targetFile.mkdirs();  
        }  
        //保存  
        try {  
            file.transferTo(targetFile); 
            return request.getContextPath()+"/upload/"+fileName;
        } catch (Exception e) {  
            e.printStackTrace();  
        }
		return null;
	}
	
	
	public List<String> uploads( MultipartFile[] files){
		List<String> list=new ArrayList<String>();
		 //判断file数组不能为空并且长度大于0  
        if(files!=null&&files.length>0){  
            //循环获取file数组中得文件  
            for(int i = 0;i<files.length;i++){  
                MultipartFile file = files[i];  
                //保存文件  
               String strname= upload(file);
               list.add(strname);
            }
            return list;
        }
        return null;
	}
	
	
	
	
	
	/*** 
     * 保存文件 
     * @param file 
     * @return 
     */  
    private boolean saveFile(MultipartFile file) {  
        // 判断文件是否为空  
        if (!file.isEmpty()) {  
            try {  
                // 文件保存路径  
                String filePath = request.getSession().getServletContext().getRealPath("/") + "upload/"  
                        + getUUID()+getExtName(file); 
                
                File targetFile = new File(request.getSession().getServletContext().getRealPath("upload"));  
                if(!targetFile.exists()){  
                    targetFile.mkdirs();  
                }  
                if(!targetFile.exists()){  
                    targetFile.mkdirs();  
                }  
                
                // 转存文件  
                file.transferTo(new File(filePath));  
                return true;  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
        }  
        return false;  
    }
    
    
    /** 
     * 获得一个UUID 
     * @return String UUID 
     */ 
    public  String getUUID(){ 
        String s = UUID.randomUUID().toString(); 
        //去掉“-”符号 
        return s.substring(0,8)+s.substring(9,13)+s.substring(14,18)+s.substring(19,23)+s.substring(24); 
    }
    
    
    /**
     * 获取文件扩展名
     * @param file
     * @return
     */
    public String getExtName(MultipartFile file){
    	String extName = ""; // 扩展名格式：
    	if (file.getOriginalFilename().lastIndexOf(".") >= 0){
			extName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
		}
    	return extName;
    }
    

}
